package com.ocom.payconfig.controller;

import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfAccountInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.service.XfAccountInfoService;
import com.ocom.payconfig.service.XfWhiteInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/7/15
 **/
@Slf4j
@RestController
public class WhiteController {

	@Autowired
	private XfWhiteInfoService xfWhiteInfoService;

	@Autowired
	private XfAccountInfoService xfAccountInfoService;

	@PostMapping(PayConfigUrl.Url_insertWhiteInfo)
	public Result White(Long companyId, Long pId, @RequestParam(value = "status" ,defaultValue = "0",required = false)Long state){

		EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.GetXFAccount(companyId, pId.intValue(), -1);
		if (entityXfAccountInfo != null && entityXfAccountInfo.getAccountState() == 1 && state == 0l) {
			return ResultUtil.success();
		}
		if (entityXfAccountInfo != null && entityXfAccountInfo.getAccountState() == 0 && state == 1l) {
			return ResultUtil.success();
		}

		try {
			//新增白名单：用comId和pId去查询白名单表xf_white_info是否为空
			//a：不为空，删除消费白名单表xf_white_info所有com_id、p_id匹配的记录。
			//b：为空执行下一步
			//   用com_id=@comId，p_id=@pId，state=0，create_time=系统当前时间，新增入库
			xfWhiteInfoService.insertWhiteInfo(companyId,pId,state);
		}catch (Exception exception){
			log.info("新增消费白名单失败 exception{{}} ",exception.getMessage());
			return ResultUtil.error(ResultEnum.Err_807);
		}
		return ResultUtil.success();
	}

	@PostMapping(PayConfigUrl.Url_getXFWhiteVer)
	public Result getXFWhiteVer(Long comId){
		return ResultUtil.success(xfWhiteInfoService.getXFWhiteVer(comId));
	}
}
